﻿using UnityEngine;
using System.Collections;


namespace UI
{
    /// <summary>
    /// 英雄物品装备等可以通用的图标
    /// </summary>
    public class CommonIcon : MonoBehaviour
    {
        public TextureKit itemIcon;
        public TextureKit heroIcon;
        public UISprite heroBack;
        public UISprite frame;
        public UILabel itemCount;
        public GameObject mustPic;
        public string id;
        public int number;
        public UIItemType type;
        public object data;
        public int quality;
        public bool clickDesc;

        public void SetDark() {
            switch (type)
            {
                case UIItemType.NONE:
                    break;
                case UIItemType.Hero:
                    heroIcon.texture.color = new Color(0.3f, 0.3f, 0.3f);
                    frame.color = new Color(0.3f, 0.3f, 0.3f);
                    heroBack.color = new Color(0.3f, 0.3f, 0.3f);
                    break;
                case UIItemType.HeroFrag:
                    heroIcon.texture.color = new Color(0.3f, 0.3f, 0.3f);
                    frame.color = new Color(0.3f, 0.3f, 0.3f);
                    heroBack.color = new Color(0.3f, 0.3f, 0.3f);
                    break;
                case UIItemType.Equip:
                    itemIcon.texture.color = new Color(0.3f, 0.3f, 0.3f);
                    break;
                case UIItemType.EquipFrag:
                    itemIcon.texture.color = new Color(0.3f, 0.3f, 0.3f);
                    frame.color = new Color(0.3f, 0.3f, 0.3f);
                    break;
                case UIItemType.Treasure:
                    itemIcon.texture.color = new Color(0.3f, 0.3f, 0.3f);
                    break;
                case UIItemType.TreasureFrag:
                    itemIcon.texture.color = new Color(0.3f, 0.3f, 0.3f);
                    frame.color = new Color(0.3f, 0.3f, 0.3f);
                    break;
                case UIItemType.Item:
                    itemIcon.texture.color = new Color(0.3f, 0.3f, 0.3f);
                    break;
                case UIItemType.Mine:
                    itemIcon.texture.color = new Color(0.3f, 0.3f, 0.3f);
                    break;
                case UIItemType.Fish:
                    itemIcon.texture.color = new Color(0.3f, 0.3f, 0.3f);
                    break;
                case UIItemType.Coin:
                    itemIcon.texture.color = new Color(0.3f, 0.3f, 0.3f);
                    break;
                case UIItemType.Diamond:
                    itemIcon.texture.color = new Color(0.3f, 0.3f, 0.3f);
                    break;
                case UIItemType.Light:
                    itemIcon.texture.color = new Color(0.3f, 0.3f, 0.3f);
                    break;
                case UIItemType.exp:
                    itemIcon.texture.color = new Color(0.3f, 0.3f, 0.3f);
                    break;
            }
        }


        /// <summary>
        /// 设置icon数据
        /// </summary>
        /// <param name="id">物品id</param>
        /// <param name="number">-1表示不显示数量</param>
        /// <param name="data">设置额外的内部数据</param>
        public virtual void SetData(string id,bool clickDesc,int number=-1,object data = null)
        {
            this.id = id;
            this.clickDesc = clickDesc;
            this.number = number;
            this.data = data;
            if (number < 0)
            {
                itemCount.gameObject.SetActive(false);
            }
            else
            {
                itemCount.gameObject.SetActive(true);
                itemCount.text = number.ToString();
            }
            string iconName;
            CommonMethod.IconParse(id, out type, out iconName, out quality);

            //不能识别的id
            if (iconName == "")
                return;

            if(type == UIItemType.Hero || type == UIItemType.HeroFrag)
            {
                heroIcon.gameObject.SetActive(true);
                heroBack.gameObject.SetActive(true);
                itemIcon.gameObject.SetActive(false);
            }
            else
            {
                heroIcon.gameObject.SetActive(false);
                heroBack.gameObject.SetActive(false);
                itemIcon.gameObject.SetActive(true);
            }
            switch(type)
            {
                case UIItemType.NONE:
                    break;
                case UIItemType.Hero:
                    heroIcon.textureName = iconName;
                    frame.gameObject.SetActive(true);
                    heroBack.spriteName = CommonMethod.GetQualityBack((QualityValue)quality);
                    frame.spriteName = CommonMethod.GetQualityFrame((QualityValue)quality);
                    break;
                case UIItemType.HeroFrag:
                    heroIcon.textureName = iconName;
                    frame.gameObject.SetActive(true);
                    heroBack.spriteName = CommonMethod.GetQualityBack((QualityValue)quality);
                    frame.spriteName = CommonMethod.GetQualityFrag((QualityValue)quality);
                    break;
                case UIItemType.Equip:
                    frame.gameObject.SetActive(false);
                    itemIcon.iconType = TextureIcon.EQUIP;
                    UIEquipment ue = data as UIEquipment;
                    if (data != null)
                    {
                        itemIcon.textureName = ue.iconPath.Substring(ue.iconPath.LastIndexOf('/') + 1);
                    }
                    else
                    {
                        itemIcon.textureName = iconName;
                    }
                    break;
                case UIItemType.EquipFrag:
                    frame.gameObject.SetActive(true);
                    frame.spriteName = CommonMethod.GetQualityFrag((QualityValue)quality);
                    itemIcon.iconType = TextureIcon.EQUIP;
                    itemIcon.textureName = iconName;
                    break;
                case UIItemType.Treasure:
                    frame.gameObject.SetActive(false);
                    itemIcon.iconType = TextureIcon.TREASURE;
                    itemIcon.textureName = iconName;
                    break;
                case UIItemType.TreasureFrag:
                    frame.gameObject.SetActive(true);
                    frame.spriteName = CommonMethod.GetQualityFrag((QualityValue)quality);
                    itemIcon.iconType = TextureIcon.TREASURE;
                    itemIcon.textureName = iconName;
                    break;
                case UIItemType.Item:
                    frame.gameObject.SetActive(false);
                    itemIcon.iconType = TextureIcon.ITEM;
                    itemIcon.textureName = iconName;
                    break;
                case UIItemType.Mine:
                    frame.gameObject.SetActive(false);
                    itemIcon.iconType = TextureIcon.MINE;
                    itemIcon.textureName = iconName;
                    break;
                case UIItemType.Fish:
                    frame.gameObject.SetActive(false);
                    itemIcon.iconType = TextureIcon.FISH;
                    itemIcon.textureName = iconName;
                    break;
                case UIItemType.Coin:
                    itemIcon.iconType = TextureIcon.ITEM;
                    frame.gameObject.SetActive(false);
                    itemIcon.textureName = iconName;
                    break;
                case UIItemType.Diamond:
                    itemIcon.iconType = TextureIcon.ITEM;
                    frame.gameObject.SetActive(false);
                    itemIcon.textureName = iconName;
                    break;
                case UIItemType.Light:
                    itemIcon.iconType = TextureIcon.ITEM;
                    frame.gameObject.SetActive(false);
                    itemIcon.textureName = iconName;
                    break;
                case UIItemType.Popularity:
                    itemIcon.iconType = TextureIcon.ITEM;
                    frame.gameObject.SetActive(false);
                    itemIcon.textureName = iconName;
                    break;
                case UIItemType.exp:
                    itemIcon.iconType = TextureIcon.ITEM;
                    frame.gameObject.SetActive(false);
                    itemIcon.textureName = iconName;
                    break;
                case UIItemType.Score:
                    itemIcon.iconType = TextureIcon.ITEM;
                    frame.gameObject.SetActive(false);
                    itemIcon.textureName = iconName;
                    break;
                case UIItemType.Point:
                    itemIcon.iconType = TextureIcon.ITEM;
                    frame.gameObject.SetActive(false);
                    itemIcon.textureName = iconName;
                    break;
                case UIItemType.Contribution:
                    itemIcon.iconType = TextureIcon.ITEM;
                    frame.gameObject.SetActive(false);
                    itemIcon.textureName = iconName;
                    break;
            }
            if(number == -1)
            {
                itemCount.gameObject.SetActive(false);
            }
            else
            {
                itemCount.gameObject.SetActive(true);
                itemCount.text = number.ToString();
            }
            if (mustPic != null)
            {
                mustPic.gameObject.SetActive(false);
            }
        }

        public void setMustPicVisable()
        {
            if (mustPic != null)
            {
                mustPic.gameObject.SetActive(true);
            }
        }
        public virtual void OnClick()
        {
            if(clickDesc)
            {
                CommonMethod.ShowDescription(type, id);
            }
        }
    }
}